package cn.tedu.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.dao.persist.repositoy.IWorkExperienceRepository;
import cn.tedu.pojo.entity.WorkExperience;
import cn.tedu.pojo.param.WorkExperienceParam;
import cn.tedu.pojo.vo.UserDetailsVO;
import cn.tedu.pojo.vo.WorkExperienceVO;
import cn.tedu.service.IWorkExperienceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkExperienceServiceImpl implements IWorkExperienceService {

    @Autowired
    private IWorkExperienceRepository workExperienceRepository;

    @Override
    public WorkExperienceVO getStandard(Long id) {
        return workExperienceRepository.getStandard(id);
    }

    @Override
    public int addNew(CurrentPrincipal currentPrincipal, WorkExperienceParam workExperienceParam) {
        WorkExperience workExperience = new WorkExperience();
        BeanUtils.copyProperties(workExperienceParam,workExperience);
        workExperience.setUserId(currentPrincipal.getId());
        return workExperienceRepository.insert(workExperience);
    }

    @Override
    public void updateById(Long id,WorkExperienceParam workExperienceParam) {
        WorkExperience workExperience = new WorkExperience();
        BeanUtils.copyProperties(workExperienceParam,workExperience);
        workExperience.setId(id);
        int rows = workExperienceRepository.updateById(workExperience);
        if (rows != 1){
            String message = "修改工作经历失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public List<WorkExperienceVO> list(Long userId) {
        return workExperienceRepository.list(userId);
    }
}
